﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublishRepository
{
	public class ConfigEntity
	{
		public string WebCodeFolder { get; set; }
		public string ApiCodeFolder { get; set; }
		public string CoreFolder { get; set; }
		public string TaskLocation { get; set; }
		public List<string> LastSelected { get; set; } = new List<string>();
		public List<ServiceEntity> Services = new List<ServiceEntity>();

		public static ConfigEntity Load()
		{
			if (!File.Exists(JSONFile))
			{
				string json = JsonConvert.SerializeObject(Default);
				File.WriteAllText(JSONFile, json);
				return Default;
			}
			using (StreamReader r = new StreamReader(JSONFile))
			{
				string json = r.ReadToEnd();
				return JsonConvert.DeserializeObject<ConfigEntity>(json);
			}
		}

		public static void Save(ConfigEntity configEntity)
		{
			string json = JsonConvert.SerializeObject(configEntity);
			File.WriteAllText(JSONFile, json);
		}

		private static string JSONFile
		{
			get
			{
				return Path.Combine(Environment.CurrentDirectory, "config.json");
			}
		}

		private static ConfigEntity Default
		{
			get
			{
				return new ConfigEntity()
				{
					WebCodeFolder = @"C:\DevelopmentGit\routefinder-web",
					ApiCodeFolder = @"C:\DevelopmentGit\RoutefinderApi",
					CoreFolder = @"C:\DevelopmentGit\core",
					TaskLocation = @"C:\DevelopmentGit\routefinderweb-watch",
					Services = new List<ServiceEntity>()
					{
						new ServiceEntity()
						{
							ServiceName="devmulti06-web",
							Url="http://devmulti06.hosted.transfinder.com/RoutefinderSW/api/upload/api",
							UnzipFolder="C:\\Google Drive\\TFDeployment\\RoutefinderPlus\\build",
							Type=ServiceType.web
						},
						new ServiceEntity()
						{
							ServiceName="devmulti06-Api",
							Url="http://devmulti06.hosted.transfinder.com/RoutefinderSW/api/upload/api",
							UnzipFolder="C:\\Google Drive\\TFDeployment\\RoutefinderApiSW\\RoutefinderApi\\bin",
							Type=ServiceType.api
						},
						new ServiceEntity()
						{
							ServiceName="serviceplus01-web",
							Url="http://serviceplus01.transfinder.com/publish/api/upload/api",
							UnzipFolder="C:\\Google Drive\\TFDeployment\\Routefinderplus\\build",
							Type=ServiceType.web
						},
						new ServiceEntity()
						{
							ServiceName="serviceplus01-Api",
							Url="http://serviceplus01.transfinder.com/publish/api/upload/api",
							UnzipFolder="C:\\Google Drive\\TFDeployment\\RoutefinderApiV2\\bin",
							Type=ServiceType.api
						},
						new ServiceEntity()
						{
							ServiceName="SalesPlus02-web",
							Url="http://SalesPlus02.transfinder.com/pubish/api/upload/api",
							UnzipFolder="C:\\Google Drive\\TFDeployment\\Routefinderplus\\build",
							Type=ServiceType.web
						},
						new ServiceEntity()
						{
							ServiceName="SalesPlus02-Api",
							Url="http://SalesPlus02.transfinder.com/pubish/api/upload/api",
							UnzipFolder="C:\\Google Drive\\TFDeployment\\RoutefinderApiV2\\bin",
							Type=ServiceType.api
						}
					}
				};
			}
		}

	}

	public class ServiceEntity
	{
		public string ServiceName { get; set; }
		public string Url { get; set; }
		public string UnzipFolder { get; set; }
		public string Type { get; set; }
	}

	public static class ServiceType
	{
		public const string web = "web";
		public const string api = "api";
	}
}
